import utils from '../core/utils';
import logger from '../core/logger';
import apiInfo from './core/apiInfo';
import current from '../core/current';
import dataInfo from './core/dataInfo';
import apiProvider from './core/apiProvider';
import { clientBase } from './core/clientBase';
import returnModel from '../model/common/returnModel';
import favBoxModel from '../model/videos/favboxModel';
import { emptyData } from '../model/common/emptyData';

class introductionClient extends clientBase {
  constructor() {
    super();
  }

  attention(mid: number, mode: string): Promise<returnModel<emptyData>> {
    return new Promise((resolve) => {
      try {
        let info = new dataInfo(`act=${mode}&fid=${mid}&re_src=32`);
        this.client.postResult("https://api.bilibili.com/x/relation/modify", info).then((res) => {
          if (res.code == 0) {
            let data: emptyData = res.data;
            resolve(this.buildSuccessResult<emptyData>(data));
          } else
            resolve(this.buildFailedResult<emptyData>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<emptyData>(e));
      }
    });
  }

  like(aid: string): Promise<returnModel<emptyData>> {
    return new Promise((resolve) => {
      try {
        let info = new dataInfo(`dislike=0&like=0&aid=${aid}`);
        this.client.postResult("https://app.bilibili.com/x/v2/view/like", info).then((res) => {
          if (res.code == 0) {
            let data: emptyData = res.data;
            resolve(this.buildSuccessResult<emptyData>(data));
          } else
            resolve(this.buildFailedResult<emptyData>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<emptyData>(e));
      }
    });
  }

  disLike(aid: string): Promise<returnModel<emptyData>> {
    return new Promise((resolve) => {
      try {
        let info = new dataInfo(`aid=${aid}&dislike=0&like=0`);
        this.client.postResult("https://app.bilibili.com/x/v2/view/dislike", info).then((res) => {
          if (res.code == 0) {
            let data: emptyData = res.data;
            resolve(this.buildSuccessResult<emptyData>(data));
          } else
            resolve(this.buildFailedResult<emptyData>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<emptyData>(e));
      }
    });
  }

  triple(aid: string): Promise<returnModel<emptyData>> {
    return new Promise((resolve) => {
      try {
        let info = new dataInfo(`aid=${aid}`);
        this.client.postResult("https://app.bilibili.com/x/v2/view/triple", info).then((res) => {
          if (res.code == 0) {
            let data: emptyData = res.data;
            resolve(this.buildSuccessResult<emptyData>(data));
          } else
            resolve(this.buildFailedResult<emptyData>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<emptyData>(e));
      }
    });
  }

  collect(aid: string, fid: number): Promise<returnModel<emptyData>> {
    return new Promise((resolve) => {
      try {
        let info = new dataInfo(`aid=${aid}&fid=${fid}&re_src=90`);
        this.client.postResult("http://api.bilibili.com/x/v2/fav/video/add", info).then((res) => {
          if (res.code == 0) {
            let data: emptyData = res.data;
            resolve(this.buildSuccessResult<emptyData>(data));
          } else
            resolve(this.buildFailedResult<emptyData>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<emptyData>(e));
      }
    });
  }

  submitCoin(count: number, aid: string): Promise<returnModel<emptyData>> {
    return new Promise((resolve) => {
      try {
        let info = new dataInfo(`aid=${aid}&from=7&mid=${current.instance.mid}&multiply=${count}`);
        info.buildVer = apiProvider.Build5;
        this.client.postResult("https://app.bilibili.com/x/v2/view/coin/add", info).then((res) => {
          if (res.code == 0) {
            let data: emptyData = res.data;
            resolve(this.buildSuccessResult<emptyData>(data));
          } else
            resolve(this.buildFailedResult<emptyData>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<emptyData>(e));
      }
    });
  }

  getFavBox(mid: number): Promise<returnModel<Array<favBoxModel>>> {
    return new Promise((resolve) => {
      try {
        let info = new apiInfo('http://api.bilibili.com/x/v3/fav/folder/space/v2', `up_mid=${mid}`);
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            let data: Array<favBoxModel> = [];
            data.push(res.data.default_folder.folder_detail);
            let arr: Array<any> = res.data.space_infos;
            arr.forEach((i) => {
              if (!utils.isEmpty(i.mediaListResponse.list))
                (i.mediaListResponse.list as Array<any>).forEach((o) => data.push(o));
            });
            resolve(this.buildSuccessResult<Array<favBoxModel>>(data));
          } else
            resolve(this.buildFailedResult<Array<favBoxModel>>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<Array<favBoxModel>>(e));
      }
    });
  }
}
export default new introductionClient();